package com.big.common.core.result;

import lombok.Data;
import lombok.experimental.Accessors;

import java.io.Serializable;

/**
 * <p>
 * 统一API响应结果封装类，包含状态码、消息和数据
 * </p>
 *
 * @author 山归岿
 * @create 2025-06-15-19:42
 */
@Data
@Accessors(chain = true)
public class Result<T> implements Serializable {
    private static final long serialVersionUID = 1L;

    /**
     * 状态码
     */
    private int code;

    /**
     * 消息
     */
    private String message;

    /**
     * 数据
     */
    private T data;

    /**
     * 是否成功
     */
    private boolean success;

    /**
     * 时间戳
     */
    private long timestamp;

    public Result() {
        this.timestamp = System.currentTimeMillis();
    }

    public static <T> Result<T> ok() {
        return ok(null);
    }

    public static <T> Result<T> ok(T data) {
        return restResult(data, ResultCode.SUCCESS.getCode(), ResultCode.SUCCESS.getMessage(), true);
    }

    public static <T> Result<T> ok(T data,String msg) {
        return restResult(data, ResultCode.SUCCESS.getCode(), msg, true);
    }

    public static <T> Result<T> failed() {
        return failed(ResultCode.ERROR);
    }

    public static <T> Result<T> failed(String message) {
        return restResult(null, ResultCode.ERROR.getCode(), message, false);
    }

    public static <T> Result<T> failed(IResultCode resultCode) {
        return restResult(null, resultCode.getCode(), resultCode.getMessage(), false);
    }

    public static <T> Result<T> failed(IResultCode resultCode, String message) {
        return restResult(null, resultCode.getCode(), message, false);
    }

    public static <T> Result<T> restResult(T data, int code, String msg, boolean success) {
        Result<T> apiR = new Result<>();
        apiR.setCode(code);
        apiR.setData(data);
        apiR.setMessage(msg);
        apiR.setSuccess(success);
        return apiR;
    }
}